package com.hjx.windowtool;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.hjx.windowtool.db.Tool;
import com.hjx.windowtool.photo2video.JavacvPhoto2VideoApp;
import com.hjx.windowtool.util.ClasspathPackageScanner;

import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.awt.event.ActionEvent;

public class WindowsToolApp {

	private JFrame frmWindow;
	private JPanel panel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					WindowsToolApp window = new WindowsToolApp();
					window.frmWindow.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public WindowsToolApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmWindow = new JFrame();
		frmWindow.setTitle("Window工具");
		frmWindow.setBounds(100, 100, 450, 300);
		frmWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		panel = new JPanel();
		frmWindow.getContentPane().add(panel, BorderLayout.CENTER);
		
		init();
	}

	private void init() {
		Package package1 = getClass().getPackage();
		System.out.println(package1.getName());
		try {
			List<String> classlist = new ClasspathPackageScanner(package1.getName()).getFullyQualifiedClassNameList();
			InputStream inputStream = getClass().getClassLoader().getResourceAsStream("classes.txt");
			BufferedReader br=new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
			String s=null;
			while((s=br.readLine())!=null) {
				classlist.add(s);
				System.out.println(s);
			}
			br.close();
			
			List<Class<?>> listTool=new ArrayList<>();
			for (String t : classlist) {
				System.out.println(t.contains("$")+"=="+t);
				if (t.contains("$")) {
					continue;
				}
				try {
					Class<?> forName = Class.forName(t);
					Tool tool = forName.getAnnotation(Tool.class);
					System.out.println(tool);
					if (tool!=null) {
						listTool.add(forName);
					}
				} catch (ClassNotFoundException e) {
					System.out.println(e.toString());
				}
			}
			System.out.println(listTool);
			for (Class<?> class1 : listTool) {
				Tool annotation = class1.getAnnotation(Tool.class);
				JButton button = new JButton(annotation.value());
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						try {
							Object object = class1.newInstance();
							Method method = class1.getMethod(annotation.method(),String[].class);
							if (method!=null) {
								Object o=new String[] {};
								method.invoke(object, o);
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
				});
				panel.add(button);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
